// pages/layout/orders/publish/index.js
const utils = require('../../../utils/util')
const app = getApp()
const ip=app.globalData.ip
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: [],
    showSelectFlag: false,
    tips: false,
    showTitlePopupTag: false,
    publishOrderTitle: [],
     title: '',//派单类型
    shootingStartTime: '',
    shootingEndTime: '',
    nowDate: '',
    titleIndex: 0,
    travelExpenseArray: [
      '包含',
      '不包含',
    ],
    detailedDescription: [
      '航拍',
      '地拍',
      '后期制作'
    ],
    priceFlag: false,
    flagAsk: true,
    payFlag: '',
    need: true, //默认需要接单保证金 
    shootingAddress: '',
    shootingAskFor: '',
    travelExpense: '包含',
    Description: '航拍',
    cashDeposit: '0',
    details: '',
    shootingMoney: '',
    phone: '',
    travelExpenseIndex: 0,
    DescriptionIndex: 0,
    deposit: app.globalData.deposit,
    openid: '',
    locationStr: '',//地址
    sendFlag: '',
    storeId: '',
    radio: '0',
    radio_flag: false,
    currentRadioValue: '0',
    //地图
    mapaddress:'',
    maplatitude:'',
    maplongitude:'',
    mapname:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */

  openPriceTable() {
    this.setData({
      priceFlag: true
    })
  },
  onLoad: function (options) {
    // let sendFlag = options.sendFlag ? options.sendFlag : 0
    // let storeId = options.storeid ? options.storeid : 0

    // console.log("本次进入" + sendFlag + storeId)
    console.log(options.storeid)
    let time = this.format_Date(utils.formatDate())
    this.setData({
      storeId:options.storeid,
      publishOrderTitle: app.globalData.publishOrderTitle,
      title: app.globalData.publishOrderTitle[0],
      shootingStartTime: time,
      shootingEndTime: time,
      nowDate: time,
    })
  },
  gotoPosition() {
    let that = this
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onChange(event) {
    console.log(event.detail)
    let arrAsk = event.detail
    let StrAsk = arrAsk.join('、')
    console.log(StrAsk)
    this.setData({
      result: event.detail,
      Description: StrAsk
    });

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  showTitlePopup() {
    this.setData({
      showTitlePopupTag: true
    })
  },
  closeTitlePopup() {
    this.setData({
      showTitlePopupTag: false
    })
  },
  titleClick(e) {
    this.setData({
      titleIndex: e.detail.value,
      title: this.data.publishOrderTitle[e.detail.value]
    })
  },
  travelExpenseClick(e) {
    this.setData({
      travelExpenseIndex: e.detail.value,
      travelExpense: this.data.travelExpenseArray[e.detail.value]
    })
  },

  // DescriptionClick(e){
  //   console.log(e)

  //   if(this.data.flagAsk){
  //     Notify({ type: 'primary', message: 'tips:拍摄要求可以多选' });
  //     this.setData({
  //       DescriptionIndex: e.detail.value,
  //       Description: this.data.detailedDescription[e.detail.value],
  //       flagAsk:false
  //     })
  //   }else{
  //   this.setData({
  //     DescriptionIndex: e.detail.value,
  //     Description: this.data.Description+ ' | ' +this.data.detailedDescription[e.detail.value]
  //   })}
  // },
  showTips() {
    this.setData({
      tips: !this.data.tips
    })
  },
  
  format_Date(curTime) {
    let time1 = new Date(curTime)
    var Y = time1.getFullYear();
    //获取月份  
    var M = (time1.getMonth() + 1 < 10 ? '0' + (time1.getMonth() + 1) : time1.getMonth() + 1);
    //获取当日日期 
    var D = time1.getDate() < 10 ? '0' + time1.getDate() : time1.getDate();
    let time = Y + '-' + M + '-' + D + ''
    return time
  },

  format_back_date(time) {
    let time1 = time.replace('年', '-').replace('月', '-').replace('日', '')
    return time1
  },
  bindDateChange(e) {

    let curTime = e.detail.value
    let time = this.format_Date(curTime)

    this.setData({
      shootingStartTime: time
    })
  },
  bindDateChange1(e) {
    let curTime = e.detail.value
    let time = this.format_Date(curTime)

    let oldTime = this.format_back_date(this.data.shootingStartTime)
    console.log(oldTime)
    let startT = new Date(oldTime)
    let endT = new Date(curTime)
    console.log(startT, endT)
    if (startT.getTime() > endT.getTime()) {
      utils.alert("结束时间不能早于开始时间！")
    } else {
      this.setData({
        shootingEndTime: time
      })
    }

  },
  setShootingAddress(e) {
    this.setData({
      shootingAddress: e.detail.value
    })
  },
  setShootingAskFor(e) {
    this.setData({
      shootingAskFor: e.detail.value
    })
  },
  closeCheckBox() { //点击textarea后关闭 checkbox组
    this.setData({
      showSelectFlag: false
    })
  },
  getPhone() {

  },
  GetOwnPhone() {
    let that = this
    request({
      url: authority.userInfo,
      data: {
        userId: utils.getToken()
      },
      method: 'post',
      success(res) {
        console.log(res)
        if (res.data.userPhone == '' || res.data.userPhone == null) {
          // that.setData({
          //   phoneFlag: true,

          // })
          wx.navigateTo({
            url: '/pages/login/bindingPhone/index',
          })
        } else {
          that.setData({
            phone: res.data.userPhone,
            phoneFlag: false,

          })
        }
      }
    })
  },
  setDetail(e) {

    this.setData({
      details: e.detail.value
    })
  },
  moneyFilter(e) {
    let value = parseInt(e.detail.value)
    if (value < 500) {
      utils.alert("拍摄费用不得小于500元")
    } else if (value > 50000) {
      utils.alert("拍摄费用若超过50000请联系客服")
    }
  },
  clearAsk() {
    this.setData({
      Description: '航拍',
      flagAsk: true
    })
  },
  setShootingMoney(e) {
    this.setData({
      shootingMoney: e.detail.value
    })
  },
  setPhone(e) {

    this.setData({
      phone: e.detail.value
    })
  },
  setNoNeed(e) {
    console.log(e.detail.value)
    console.log(e)
    this.setData({
      noNeed: true,
      need: false,
      cashDeposit: e.detail.value
    })
  },
  cashDepositChange(e) {
    console.log(e.detail.value)
    this.setData({
      cashDeposit: e.detail.value
    })
  },
  releaseSell1() {


  },
  releaseSell2() {
    wx.showToast({
      title: '取消支付',
      icon: 'none',
      duration: 1000 //持续的时间
    })
  },
  sendSellShop() {
    
  },
  releaseSell() {
    
  },
  changeRadio(e) {
    console.log(e.currentTarget.dataset.value)
    let nowRadioValue = e.currentTarget.dataset.value
    if (this.data.radio_flag == false && '0' != nowRadioValue) {
      this.setData({
        radio_flag: true,
      })
    } else {
      if ('1' != nowRadioValue) {
        this.setData({
          radio_flag: false,
        })
      }
    }
    this.setData({
      currentRadioValue: nowRadioValue
    })
  },

  showSelectArea() {
    this.setData({
      showSelectFlag: !this.data.showSelectFlag
    })
  },
  btnHandle() {
    wx.request({
      url: ip+'/store/OrderStore',
      data:{
        storeId:this.data.storeId,
        userId:wx.getStorageSync('userid'),
        shootingAddress:this.data.mapaddress+','+this.data.mapname+','+this.data.maplatitude+','+this.data.maplongitude,
        shootingTimeStart:this.data.shootingStartTime,
        shootingTimeEnd:this.data.shootingEndTime,
        shootingAskFor:this.data.Description,
        details:this.data.details,
        costTotal:this.data.shootingMoney,
        phone:this.data.phone
      },
      method:"POST",
      header:{
        "Content-Type": "application/json"
      },
      success:res=>{
        console.log(res);
        if(res.data.code==-1){
          wx.showToast({
            title: res.data.msg,
            icon:'none',
            duration: 1500,  // 提示窗停留时间，默认1500ms
          })
          return 0
        }
        wx.showToast({
          title: '操作成功！', // 标题
          icon: 'success',  // 图标类型，默认success
          duration: 40000,  // 提示窗停留时间，默认1500ms
          success(){
            wx.switchTab({
              url: '../../homePage/index',
            })
          }
        })
      }
    })
  },
  orderLocation(res){
    wx.chooseLocation({
      success:res=>{
        console.log(res)
        this.setData({
          mapaddress:res.address,
          maplatitude:res.latitude,
          maplongitude:res.longitude,
          mapname:res.name
        })
        /*
            mapaddress:'',
    maplatitude:'',
    maplongitude:'',
    mapname:'',
        */
      }
    })
// let plugin = requirePlugin('routePlan');
// let key ='456BZ-4YAER-CQQW4-WU6ST-3KHKT-DPFVJ'; //使用在腾讯位置服务申请的key
// let referer ='xiangpai';  //调用插件的小程序的名称
// let startPoint = JSON.stringify({ //起点
//   'name':'中国技术交易大厦',
//   'latitude': 39.984154,
//   'longitude': 116.30749
// });
// let endPoint = JSON.stringify({ //终点
//   'name':'北京西站',
//   'latitude': 39.894806,
//   'longitude': 116.321592
// });
// wx.navigateTo({
//   url:'plugin://routePlan/route-plan?key=' + key +'&referer=' + referer +'&endPoint=' + endPoint
// });
    // console.log(res.detail.value)
    // this.setData({
    //   locationStr:res.detail.value
    // })
  }
})